<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/30
 * Time: 17:57
 */

namespace App\Http\Controllers\Backstage;


use App\Http\Controllers\Controller;
use App\Model\Backstage\CustomerFollowSourde;
use App\Model\Backstage\CustomersFollow;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Validator;
use App\Model\Backstage\RoomFollow;
use App\Tool\Error;
use Illuminate\Support\Facades\Auth;

/**
 * Class CustomerFollowController
 * @package App\Http\Controllers\Backstage
 * 客源跟进控制器
 */
class CustomerFollowController extends Controller
{

    //添加客源跟进
    public function addCustomerFollow(Request $request, CustomersFollow $customersfollow, CustomerFollowSourde $customerfollowsourde)
    {
        $cfsid = true;$r_id = true;
        $params = $request->all();
        $validator = Validator::make($params, [
            'cf_c_id'         => 'required',
            'cf_fllow_type'   => 'required',
            'cf_content'      => 'required',
        ]);
        if ($validator->fails()) {
            return $this->error(Error::PARAM_MISSING);
        }
        //开启事务
        DB::beginTransaction();

        $cfid = $customersfollow->addCustomersFollow($params);
        if($request->input('cfs_remind_time')){
            //添加提醒

            $cfsid = $customerfollowsourde->addCustomerFollowSourde($params,$cfid);//客源跟进提醒
        }

        if($params['cf_fllow_type'] == 3){//跟进方式为看房
            $RoomFollow = new RoomFollow();
           $par = $params;
           $par['f_room_id'] = $params['cf_room_id'];
           $par['f_follow_up_way'] = $params['cf_fllow_type'];
           $par['f_department_id'] = $params['cf_department_id'];
           $par['f_user_id'] = $params['cf_user_id'];
           $par['f_content'] = $params['cf_content'];

            //添加房源跟进
            $r_id = $RoomFollow->addRoomFollow($par,$params['cf_c_id']);
            //dd($r_id);
        }

        if($cfid && $cfsid && $r_id){
            DB::commit();
            return $this->success('ok', '跟进添加成功');
        } else {
            DB::rollback();
            return $this->error(Error::ADD_ERROR);
        }
    }

    /**
     * 修改客源跟进
     */
    public function updateCustomerFollow(Request $request, CustomersFollow $customersfollow){
        $params = $request->all();
        $validator = Validator::make($params, [
            'cf_id'           =>'required',//跟进ID
            'cf_fllow_type'   => 'required',//跟进方式
            'cf_content'      => 'required',//跟进内容
             'cf_user_id'     =>'required',//
            'cf_department_id'=>'required'
        ]);
        if ($validator->fails()) {
            return $this->error(Error::PARAM_MISSING);
        }
        $result = $customersfollow->updateCustomersFollow($params);
        if($result){
            return $this->success('ok', '修改成功');
        }else{
            return $this->error(Error::EDIT_ERROR);
        }
    }

    /*
     * 删除客源跟进
     * Wu Ning
     */
    public function delCustomerFollow(Request $request, CustomersFollow $customersfollow)
    {
        $followId = $request->all('cf_id');
        $rel = $customersfollow->delFollow($followId);
        if($rel){
            return $this->success('ok', '删除成功');
        }else{
            return $this->error(Error::DEL_ERROR);
        }

    }

    /**
     * 客源跟进列表
     */
    public function customerFollowlist(Request $request, CustomersFollow $customersfollow){
        $param = $request->all();
        $where = [];
        //跟进  开始时间
        if ($request->input('start_time')) {
            $where[] = ['cf_created_at', '>=', strtotime($param['start_time'].' 00:00:00')];
        } else {
            $where[] = ['cf_created_at', '>=', strtotime(date('Y-m-d',strtotime('-3 month')).' 00:00:00')];
        }
        //跟进结束时间
        if ($request->input('end_time')) {
            $where[] = ['cf_created_at', '<=', strtotime($param['end_time'].' 23:59:59')];
        } else {
            $where[] = ['cf_created_at', '<=', time()];
        }
        //类型  全部传空   跟进员工传1    房源归属人2
        if (!$request->input('people_type') || $request->input('people_type') == 1) {
            //部门ID
            if ($request->input('department_id')) {
                $where[] = ['cf_department_id',  $request->input('department_id')];
            }else{
                $where[] = ['cf_department_id',Auth::user()['u_d_id']];
            }
            //跟进人ID
            if ($request->input('u_id')) {
                $where[] = ['cf_user_id',  $request->input('u_id')];
            }else if (!$request->input('department_id') && !$request->input('u_id')){
                $where[] = ['cf_user_id',Auth::user()['u_id']];
            }
        }
        //房源归属人2
        if ($request->input('people_type') == 2) {
            //部门ID
            if ($request->input('department_id')) {
                $where[] = ['c_store_id',  $request->input('department_id')];
            }else{
                $where[] = ['c_store_id',Auth::user()['u_d_id']];
            }
            //跟进人ID
            if ($request->input('u_id')) {
                $where[] = ['c_employees_id',  $request->input('u_id')];
            }else if (!$request->input('department_id') && !$request->input('u_id')){
                $where[] = ['c_employees_id',Auth::user()['u_id']];
            }
        }

        //交易 全部 ==空 ， 求购 == 1  ， 求租  == 2，   租购  == 3
        // //   客源 451求购 452求购 453租购
        if ($request->input('trading_type') == 1) {
            $where[] = ['c_trading_type',  451];
        } elseif ($request->input('trading_type') == 2) {
            $where[] = ['c_trading_type',  452];
        } elseif ($request->input('trading_type') == 3) {
            $where[] = ['c_trading_type',  453];
        }

        //跟进方式
        if ($request->input('cf_fllow_type')) {
            $where[] = ['cf_fllow_type',  $request->input('cf_fllow_type')];
        }

        //关键词智能搜索
        $keywords = '';
        if ($request->input('keywords')){
            //跟进的内容    房源的编号  业主姓名   业主电话
            $keywords = trim($request->input('keywords'));
        }

        $list = $customersfollow->customersFollowLists(env('PAGE_LIMIT_ADMIN'), $where, $keywords);
        if (!$list) {
            $data['start'] = date('Y-m-d',strtotime('-3 month'));
            $data['end']  = date('Y-m-d',time());
            return $this->error(Error::NULL_DATA,$data);
        }
        $start = date('Y-m-d',strtotime('-3 month'));
        $end = date('Y-m-d',time());
        return $this->success('ok', $list,$start,$end);
    }


}





















